<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 6/11/24
 * Time: 6:14 PM
 */

namespace App\Console\Commands;

use App\Models\WechatAccount;
use App\Models\WechatCorp;
use App\Services\KfAccountService;
use Carbon\Carbon;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Models\WechatCustomerData as CdModel;
use Illuminate\Support\Facades\Redis;


/**
 * 客户数据统计
 *
 * Class Push
 * @package App\Console\Commands
 */
class WechatCustomerRemark extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wechat:customer_remark';


    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '客户备注';


    public function __construct()
    {
        parent::__construct();
    }


    /**
     *
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle()
    {
        Log::info('修改客户备注');
        $service = new KfAccountService();
        while (true) {
            // 并发请求企微接口，会报错
            sleep(1);

            $data   = Redis::blpop(KfAccountService::REDIS_CORP_WECHAT_CUSTOMER_REMARK, 5); // 消费修改客户备注队列
            $result = $data[1] ?? '';
            if ($result) {
                $result = json_decode($result, true);

                $corpId         = $result['corp_id'] ?? '';
                $userId         = $result['user_id'] ?? '';
                $externalUserID = $result['external_userID'] ?? '';
                $remark         = $result['remark'] ?? '';
                if ($corpId && $userId && $externalUserID && $remark) {
                    $service->setCustomerRemark($corpId, $userId, $externalUserID, $remark);
                }
            } else {
                break;
            }
        }
    }

}
